package 二叉树.顺序存储二叉树;

public class ArrBinaryTreeDemo {
    public static void main(String[] args) {
        int[] input = {1, 2, 3, 4, 5, 6, 7};
        ArrBinaryTree arrBinaryTree = new ArrBinaryTree(input);
        System.out.println("前序遍历：");
        arrBinaryTree.preOrder();
        System.out.println("中序遍历：");
        arrBinaryTree.indixOrder(0);
        System.out.println("后序遍历：");
        arrBinaryTree.suffixOrder(0);
    }
}

class ArrBinaryTree {
    private int[] arr;

    public ArrBinaryTree(int[] arr) {
        this.arr = arr;
    }

    public void preOrder(){
        preOrder(0);
    }
    public void preOrder(int index) {
        if (arr.length == 0 || arr == null) {
            System.out.println("当前数组为空，无法模拟二叉树去进行前序遍历");
            return;
        }
        System.out.println(arr[index]);
        if (arr.length > index * 2 + 1) {
            preOrder(index * 2 + 1);
        }
        if (arr.length > index * 2 + 2) {
            preOrder(index * 2 + 2);
        }
    }

    public void indixOrder(int index) {
        if (arr.length == 0 || arr == null) {
            System.out.println("当前数组为空，无法模拟二叉树去进行中序遍历");
            return;
        }
        if (arr.length > index * 2 + 1) {
            indixOrder(index * 2 + 1);
        }
        System.out.println(arr[index]);
        if (arr.length > index * 2 + 2) {
            indixOrder(index * 2 + 2);
        }
    }

    public void suffixOrder(int index) {
        if (arr.length == 0 || arr == null) {
            System.out.println("当前数组为空，无法模拟二叉树去进行后序遍历");
            return;
        }
        if (arr.length > index * 2 + 1) {
            suffixOrder(index * 2 + 1);
        }
        if (arr.length > index * 2 + 2) {
            suffixOrder(index * 2 + 2);
        }
        System.out.println(arr[index]);
    }
}
